package com.fjw.controller;

import com.fjw.pojo.Article;
import com.fjw.pojo.Result;
import com.fjw.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/add")
    public Result add(@RequestBody Article article){
        System.out.println(article);
        articleService.add(article);
        return Result.success();
    }

    @GetMapping("/findById")
    public Result<Article> findById(Integer id){
        System.out.println(id);
        Article res = articleService.findById(id);
        return Result.success(res);
    }

    @PostMapping("/findByIds")
    public Result<List<Article>> findByIds(@RequestBody List<Integer> articleIds){
        System.out.println(articleIds);
        List<Article> articles = new ArrayList<>();
        for(int i=0;i<articleIds.size();i++){
            Article res = articleService.findById(articleIds.get(i));
            articles.add(res);
        }
        System.out.println(articles);
        return Result.success(articles);
    }

    @GetMapping("/findByAuthor")
    public Result<List<Article>> findByAuthor(Integer author){
        System.out.println(author);
        List<Article> res = articleService.findByAuthor(author);
        return Result.success(res);
    }

    @GetMapping("/findByCategory")
    public Result<List<Article>> findByCategory(String job_category,String major_category){
        System.out.println(job_category);
        System.out.println(major_category);
        List<Article> res = articleService.findByCategory(job_category,major_category);
        return Result.success(res);
    }

    @GetMapping("/getAll")
    public Result<List<Article>> getAll(){
        List<Article> res = articleService.getAll();
        return Result.success(res);
    }

    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id){
        System.out.println(id);
        articleService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/updateById")
    public Result updateById(@RequestBody Article article){
        System.out.println(article);
        articleService.updateById(article);
        return Result.success();
    }

}
